# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: inkyu

import logging

from coin.exchange.base.order_gateway import OrderType
from coin.exchange.bithumb.kr_rest.product import BithumbProduct
from coin.exchange.bithumb.order_gateway import BithumbOrderGateway
from coin.strategy.executor.aggressive.aggressive_executor import AggressiveExecutor
from coin.strategy.executor.passive.simple_mm_executor import SimpleMMExecutor
from coin.strategy.executor.util.traits import (order_logger, SpotExecutorTraits)
from coin.exchange.base.order_gateway import OrderSide


class BithumbSimpleTraits(SpotExecutorTraits):
  def __init__(self, executor_config, order_gateway, logger=None):
    logger = logger or logging.getLogger('BithumbSimpleMMExecutor')
    super().__init__(executor_config, order_gateway, logger)

  def _submit_order(self, product, price, qty, order_side):
    if not self.og.submittable():
      self._logger.debug('Skip submission: %s %f@%f' % (product, qty, price))
      return False

    assert order_side in (OrderSide.BUY, OrderSide.SELL), 'Unknown order side: %s' % order_side
    self.og.submit(product, price, qty, order_side, OrderType.LIMIT)
    order_side = 'BUY' if order_side == OrderSide.BUY else 'SELL'
    self.logger.info('%s %s %s @ %s', order_side, product, qty, price)

  def _cancel_orders_impl(self, order_id_list):
    self.og.cancel_multiple(order_id_list)


class BithumbSimpleMMExecutor(SimpleMMExecutor):
  ProductType = BithumbProduct
  OGType = BithumbOrderGateway
  Traits = BithumbSimpleTraits


class BithumbSimpleAggExecutor(AggressiveExecutor):
  ProductType = BithumbProduct
  OGType = BithumbOrderGateway
  Traits = BithumbSimpleTraits
